#include<iostream>
using namespace std;
#include "../source/public.h"

framework::Logger::ptr g_logger = FRAMEWORK_LOG_ROOT();

int n = 0;
framework::RWMutex g_mutex;
framework::NullMutex g_null_mutex;

void func1()
{
    // cout << "name: " << framework::Thread::GetName()
    //      << endl << "this.name  " << framework::Thread::GetThis()->getName()
    //      << endl <<"id  " << framework::GetThreadId()
    //      << endl << "this.id  " << framework::Thread::GetThis()->getId()
    //      << endl << "=================================================" << endl;
    
    for(int i = 0; i < 100; i++)
    {
        framework::RWMutex::WriteLock lock(g_mutex);
        ++n;
    }
}

void func2()
{
    cout << "world" << endl;
    cout << "sys2: " << syscall(SYS_gettid) << endl;
    cout << "self2: " << pthread_self() << endl;
    cout << "pid2: " << getpid() << endl;
    sleep(2);
}

int main(void)
{
    FRAMEWORK_LOG_INFO(g_logger) << "thread test begin";
    std::vector<framework::Thread::ptr> thrs;
    for(int i = 0; i < 5; i++)
    {
        string str = "name_" + to_string(i);
        framework::Thread::ptr thr(new framework::Thread(&func1, str));
        thrs.push_back(thr);
    }

    for(auto t : thrs)
    {
        t->join();
    }
    cout << "n = " << n << endl;

    FRAMEWORK_LOG_INFO(g_logger) << "thread test end";
    return 0;
}
